#lang racket


(provide (struct-out color)
         col
         color-of-rgb
         rgb-of-color)


(define (color-write c port mode)
  (write-string "color" port)
  (write-string
   (format "(~A ~A ~A)" (color-red c) (color-green c) (color-blue c)) port))

(struct color (red green blue)
  #:property prop:custom-write color-write)

(define col
  (case-lambda
    ((name)
     (match name
       ('white (color 1 1 1))
       ('black (color 0 0 0))))
    ((red green blue)
     (color red green blue))))

; creates a 'color' converting 'red', 'green' and 'blue' from [0-255] to [0-1]
(define (color-of-rgb red green blue)
  (color (/ red 255) (/ green 255) (/ blue 255)))

; returns 'red', 'green' and 'blue' fields of 'col' converted from [0-1] to [0-255]
(define (rgb-of-color col)
  (values
   (* (color-red col) 255)
   (* (color-green col) 255)
   (* (color-blue col) 255)))